<%-- Validation input data, to be included in trainees.jsp, no error page. --%>

<%@ page import="com.kha.Trainee"%>

<%
	// Get signal to proceed the validation process.
	String hidden = request.getParameter("txtHidden");

	if ("input".equals(hidden)) {
		// It's time to validate.

		// Pull data from request object.
		String account = request.getParameter("txtAccount");
		String firstname = request.getParameter("txtFirstName");
		String lastname = request.getParameter("txtLastName");
		String email = request.getParameter("txtEmail");

		// Get or create bean.
		Trainee trainee = (Trainee) session.getAttribute("trainee");

		// Intialization bean if not existed (in session).
		if (trainee == null) {
			trainee = new Trainee();
			session.setAttribute("trainee", trainee);
		}

		// Update data.
		trainee.setAccount(account.trim());
		trainee.setFirstname(firstname.trim());
		trainee.setLastname(lastname.trim());
		trainee.setEmail(email.trim());

		if (account == null || "".equals(account.trim())) {

			trainee.setAccount("");

			// Notify user.
			String message = "Account";
%>
<jsp:include page="/WEB-INF/view/alert.jsp">
	<jsp:param value="<%=message%>" name="msg" />
</jsp:include>
<%
	} else {
			// Set well-formatted data.
			trainee.setAccount(account.trim());
		}

		if (firstname == null || "".equals(firstname.trim())) {

			trainee.setFirstname("");

			// Notify user.
			String message = "Firstname";
%>
<jsp:include page="/WEB-INF/view/alert.jsp">
	<jsp:param value="<%=message%>" name="msg" />
</jsp:include>
<%
	} else {
			// Set well-formatted data.
			trainee.setFirstname(firstname.trim());
		}

		if (lastname == null || "".equals(lastname.trim())) {

			trainee.setLastname("");

			// Notify user.
			String message = "Lastname";
%>
<jsp:include page="/WEB-INF/view/alert.jsp">
	<jsp:param value="<%=message%>" name="msg" />
</jsp:include>
<%
	} else {
			// Set well-formatted data.
			trainee.setLastname(lastname.trim());
		}

		if (email == null || "".equals(email.trim())) {

			trainee.setEmail("");

			// Notify user.
			String message = "Email";
%>
<jsp:include page="/WEB-INF/view/alert.jsp">
	<jsp:param value="<%=message%>" name="msg" />
</jsp:include>
<%
	} else {
			// Set well-formatted data.
			trainee.setEmail(email.trim());
		}

	}
%>